<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/5
 * Time: 上午1:01
 */

namespace Home\Controller;
use Think\Controller;

class RegisterController extends Controller {

    //注册页面
    public function register(){
        if(IS_POST){
            $user_db = D('User');
            if($user_db->create()){
                if($id = $user_db->add()){
                    $user = $user_db->getByUser_id($id);
                    session('loginUser',$user);
                    $this->success("Register Success!");
                }else{
                    $this->error("Register failed!");
                }
            }else{
                exit($user_db->getError());
            }
        }
        else {
            $this->display();
        }
    }

    //生成手机验证码
    public function getVerfiCode(){

        $result = "";
        for($i = 0;$i < 6;$i++){
            $result .= chr(mt_rand(48,57));
        }
        //dump($result);
        return $result;
    }

    //发送手机验证码
    public function sendVerfiCode(){
        $phone = $_GET['phone'];
        if(is_null($phone)){
            $this->error("手机号码为空");
        }else if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){
            $this->error("手机号码错误");
        }else{
            //验证码发送URL
            $url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
            $verfiCode = $this->getVerfiCode();
            $post_data = "account=cf_gwhz&password=XLyxR3&mobile=".$phone."&content=".rawurlencode("您的验证码是：".$verfiCode."。请不要把验证码泄露给其他人。");
            $result = xml_to_array(post($post_data,$url));
            if($result['SubmitResult']['code'] == 2){
                session('verfiCode',$verfiCode);
                $this->success('验证码已发送');
            }else{
                $this->error('验证码发送失败');
            }
        }
    }




} 